package org.sunny.security;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

@EnableWebSecurity
public class WebSecurityConfig implements WebMvcConfigurer {

    @Bean
    public WebSecurityConfigurerAdapter webSecurityConfigurerAdapter(){
        return new MyWebSecurityConfigurerAdapter();
    }

    @Bean
    public UserDetailsService userDetailsService() throws Exception {
        UserDetailsService manager = (s) -> {
            User user = new User("admin", "admin",
                    Arrays.asList(
                            new SimpleGrantedAuthority("ROLE_admin"),
                            new SimpleGrantedAuthority("ROLE_dba")
                    )
            );
            return user;
        };
//        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
//        manager.createUser(org.springframework.security.core.userdetails.User.withDefaultPasswordEncoder().username("admin").password("admin").roles("admin").build());
        return manager;
    }

    @Bean
    public PasswordEncoder getPasswordEncoder(){
        return new PasswordEncoder() {
            @Override
            public String encode(CharSequence charSequence) {
                return charSequence.toString();
            }

            @Override
            public boolean matches(CharSequence charSequence, String s) {
                return charSequence.equals(s);
            }
        };
    }
}